using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace blogapi
{
    public partial class publikacepolozky : Form
    {
        public publikacepolozky(publy pb, pages pg,servery serv,nastaveni nas)
        {
            InitializeComponent();
            pub = pb;
            pag = pg;
            ser = serv;
            nast = nas;
            panel1.Visible = false;
            label2.Text = nast.getlang("pripravujesepublikace");
        }
        publy pub;
        pages pag;
        servery ser;
        nastaveni nast;

        Thread publikacnivlakno = null;
        bool publy = true;

        private void button1_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show(nast.getlang("opravdustornovat"), nast.getlang("publikacepris"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                label2.Text = nast.getlang("storned");
                publy = false;
            }
        }

        public void odstartuj()
        {
            label2.Text = nast.getlang("pripravujesepublikace");
            publy = true;
            checkBox1.Checked = nast.automazat;
            panel1.Visible = false;
            treeView1.Nodes.Clear();
            ArrayList vybrane = new ArrayList();
            for (int i = 0; i < pub.listView1.Items.Count; i++)
            {
                if (pub.listView1.Items[i].Checked == true)
                {
                    vybrane.Add(pub.listView1.Items[i].Tag);
                }
            }
            ArrayList vybranes = new ArrayList();
            for (int i = 0; i < ser.listView1.Items.Count; i++)
            {
                if (ser.listView1.Items[i].Checked == true)
                {
                    vybranes.Add(ser.listView1.Items[i].Tag);
                }
            }
            publikacnivlakno = new Thread(delegate() { publikuj(vybrane, vybranes); });
            publikacnivlakno.IsBackground = true;
            publikacnivlakno.Priority = ThreadPriority.Highest;
            publikacnivlakno.Start();
        }

        private void publikujdolabelu(string text)
        {
            label2.Text = text;
        }
        delegate void publikujdolabeludel(string text);
        private void pridejnode(TreeNode nod)
        {
            treeView1.Nodes.Add(nod);
        }
        delegate void pridejnodedel(TreeNode nod);
        private void zobraz()
        {
            panel1.Visible = true;
        }
        delegate void zobrazdel();

        public ArrayList clonearray(ArrayList arr)
        {
            ArrayList vystup = new ArrayList();
            for (int i = 0; i < arr.Count; i++)
            {
                blogs bl = (blogs)arr[i];
                blogs vys = new blogs();
                vys.url = bl.url;
                vys.user = bl.user;
                vys.pass = bl.pass;
                vystup.Add(vys);
            }
            return vystup;
        }

        private void publikuj(ArrayList vybrane, ArrayList vybranes)
        {
            ApiHandler.PostHandler post = new ApiHandler.PostHandler();
            ArrayList pomocnejserveru = clonearray(pag.mozn);
            for (int i = 0; i < vybrane.Count; i++)
            {
                //ListViewItem itemlistvie = (ListViewItem)prispevky.Items[i];
                prispevek pris = (prispevek)pub.prispevkypole[(int)vybrane[i]];
                if (publy)
                {
                    label2.Invoke(new publikujdolabeludel(publikujdolabelu), new object[] { nast.getlang("prispevekprepare") +" " + pris.titulek });
                }
                TreeNode nod = new TreeNode(pris.titulek);
                nod.Tag = (int)vybrane[i];
                nod.Checked = true;
                for (int s = 0; s < vybranes.Count; s++)
                {
                    blogs bl = (blogs)pomocnejserveru[(int)vybranes[s]];
                    label2.Invoke(new publikujdolabeludel(publikujdolabelu), new object[] { nast.getlang("serverprepare")+" " + bl.url });
                    if (bl.pass == "" || bl.user == "")
                    {
                        prihlasovaciform prihlas = new prihlasovaciform(bl,nast);
                        prihlas.ShowDialog();
                        if (prihlas.checkBox1.Checked)
                        {
                            blogs blg =(blogs) pag.mozn[(int)vybranes[s]];
                            blg.pass = bl.pass;
                            blg.user = bl.user;
                        }
                        prihlas = null;
                    }
                    TreeNode nodd = new TreeNode(bl.url);
                    bool vys = false;
                    if (publy)
                    {
                        nodd.BackColor = Color.YellowGreen;
                        if (nast.debugmode)
                        {
                            Random nah = new Random();
                            vys = Convert.ToBoolean(nah.Next(0,2));
                        }
                        else
                        {
                            vys = post.SendPost(bl.url, bl.user, bl.pass, pris.titulek, pris.text, pris.kategorie);
                        }
                    }
                    if (vys == false)
                    {
                        nod.Checked = false;
                        nodd.BackColor = Color.HotPink;
                        if (publy)
                        {
                            label2.Invoke(new publikujdolabeludel(publikujdolabelu), new object[] { nast.getlang("chybapripublikac") });
                        }
                    }
                    nod.Nodes.Add(nodd);
                }
                treeView1.Invoke(new pridejnodedel(pridejnode),new object[]{nod});
            }
            if (publy)
            {
                label2.Invoke(new publikujdolabeludel(publikujdolabelu), new object[] { nast.getlang("generovani") });
                Thread.Sleep(1000);
            }
            panel1.Invoke(new zobrazdel(zobraz), new object[] { });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (publikacnivlakno != null)
            {
                publikacnivlakno.Abort();
                publikacnivlakno = null;
            }
            if (checkBox1.Checked)
            {
                for (int i = treeView1.Nodes.Count-1; i >=0; i--)
                {
                    if (treeView1.Nodes[i].Checked == false)
                    {
                        continue;
                    }
                    int poz = (int)treeView1.Nodes[i].Tag;
                    pub.prispevkypole.RemoveAt(poz);
                }
                pub.vypis(true);
            }
            this.Hide();
        }

        public void setlang()
        {
            label1.Text = nast.getlang("publyzprav");
            button1.Text = nast.getlang("storno");
            checkBox1.Text = nast.getlang("delvybrane");
            label3.Text = nast.getlang("vysledkypubli");
            button2.Text = nast.getlang("ok");
            Text = "W - "+nast.getlang("publikovani");
        }
    }
}